/************************************************************
 *  * Hyphenate CONFIDENTIAL
 * __________________
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Hyphenate Inc.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Hyphenate Inc.
 */

#import "EaseConversationCell.h"

#import "UIImageView+WebCache.h"

#if ENABLE_LITE == 1
#import <HyphenateLite/EMConversation.h>
#else
#import <Hyphenate/EMConversation.h>
#endif

CGFloat const EaseConversationCellPadding = 10;

@interface EaseConversationCell()

@property (nonatomic) NSLayoutConstraint *titleWithAvatarLeftConstraint;

@property (nonatomic) NSLayoutConstraint *titleWithoutAvatarLeftConstraint;

@property (nonatomic) NSLayoutConstraint *detailWithAvatarLeftConstraint;

@property (nonatomic) NSLayoutConstraint *detailWithoutAvatarLeftConstraint;

@end

@implementation EaseConversationCell

#pragma mark - setter

- (void)setModel:(id<IConversationModel>)model
{
    _model = model;
    
    self.badgeNum = _model.conversation.unreadMessagesCount;
    
    [self setUserInfo];
}

#pragma mark - class method

/*!
 @method
 @brief 获取cell的重用标识
 @ discussion
 @param model   消息model
 @return 返回cell的重用标识
 */
+ (NSString *)cellIdentifierWithModel:(id)model
{
    return @"EaseConversationCell";
}

/*!
 @method
 @brief 获取cell的高度
 @ discussion
 @ param model   消息model
 @return  返回cell的高度
 */
+ (CGFloat)cellHeightWithModel:(id)model
{
    return EaseConversationCellMinHeight;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    //    if (_avatarView.badge) {
    //        _avatarView.badgeBackgroudColor = [UIColor redColor];
    //    }
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    [super setHighlighted:highlighted animated:animated];
    //    if (_avatarView.badge) {
    //        _avatarView.badgeBackgroudColor = [UIColor redColor];
    //    }
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addObserver:self forKeyPath:@"model.userInfoModel.nickName" options:(NSKeyValueObservingOptionNew) context:nil];
        [self addObserver:self forKeyPath:@"model.userInfoModel.headPic" options:(NSKeyValueObservingOptionNew) context:nil];
        [self addObserver:self forKeyPath:@"model.userInfoModel.code" options:(NSKeyValueObservingOptionNew) context:nil];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    __weak typeof (self) weakself = self;
    if ([keyPath isEqualToString:@"model.userInfoModel.nickName"]) {
        weakself.HOTCHATtitleLabel.text = self.model.userInfoModel.nickName.length > 0 ? self.model.userInfoModel.nickName : self.model.conversation.conversationId;
    }else if ([keyPath isEqualToString:@"model.userInfoModel.headPic"]){
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",APIHEADER,weakself.model.userInfoModel.headPic];
        NSURL * url = [NSURL URLWithString:urlStr];
        [weakself.HOTCHATheadimgView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"personal_atatar_but_h"]];
    }else if ([keyPath isEqualToString:@"model.userInfoModel.code"]){
        weakself.vipImg = [weakself.model.userInfoModel.code HOTCHATgetVipStr];
    }
}
- (void)setUserInfo{
    
    self.HOTCHATtitleLabel.text = self.model.userInfoModel.nickName.length > 0 ? self.model.userInfoModel.nickName : self.model.conversation.conversationId;
    self.vipImg = [self.model.userInfoModel.code HOTCHATgetVipStr];
    
    if (self.model.userInfoModel.headPic.length > 0) {
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",APIHEADER,self.model.userInfoModel.headPic];
        NSURL * url = [NSURL URLWithString:urlStr];
        [self.HOTCHATheadimgView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"personal_atatar_but_h"]];
    }else{
        if ([self.model.avatarURLPath length] > 0){
            [self.HOTCHATheadimgView sd_setImageWithURL:[NSURL URLWithString:self.model.avatarURLPath] placeholderImage:self.model.avatarImage];
        } else {
            if (self.model.avatarImage) {
                self.HOTCHATheadimgView.image = self.model.avatarImage;
            }
        }
    }
}
- (void)dealloc{
    [self removeObserver:self forKeyPath:@"model.userInfoModel.nickName"];
    [self removeObserver:self forKeyPath:@"model.userInfoModel.headPic"];
    [self removeObserver:self forKeyPath:@"model.userInfoModel.code"];
}


@end
